package org.jxc.service;

import java.util.Date;
import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.constants.JXCString;
import org.jxc.dao.PurchaseOrderDAO;
import org.jxc.domain.PurchaseOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.dao.DAOFactory;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.LoginBean;
import org.team4.util.Validate;
import org.team4.vo.Page;

@Service
public class PurchaseOrderService implements IService {

	private PurchaseOrderDAO purchaseOrderDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		purchaseOrderDAO = (PurchaseOrderDAO) DAOFactory.getDAO(sessionFactory,
				PurchaseOrderDAO.class);
	}

	/**
	 * 分页
	 * 
	 * @param page
	 * @param superId
	 * @return
	 * @throws CRUDException
	 */
	public Page<PurchaseOrder> listPagination(Page<PurchaseOrder> page,
			String superId) throws CRUDException {
		if (Validate.notNull(superId))
			return purchaseOrderDAO
					.pagination(
							page,
							"from PurchaseOrder where deleteFlag = '0' productTypeId = ?",
							null, superId);
		else
			return purchaseOrderDAO.pagination(page,
					"from PurchaseOrder where deleteFlag = '0'", null);
	}

	/**
	 * 获取所有商品
	 * 
	 * @return
	 * @throws CRUDException
	 */
	public List<PurchaseOrder> findAllPurchaseOrder(String orderBy)
			throws CRUDException {
		List<PurchaseOrder> list = purchaseOrderDAO
				.find("from PurchaseOrder where deleteFlag='0'" + " ORDER BY "
						+ orderBy);
		return list;
	}

	/**
	 * (overload)获取所有商品,按默认规则(创建时间)排序
	 * 
	 * @return
	 * @throws CRUDException
	 */
	public List<PurchaseOrder> findAllPurchaseOrder() throws CRUDException {
		return findAllPurchaseOrder("createTime");
	}

	/**
	 * 根据id查询商品
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public PurchaseOrder findById(String id) throws Exception {
		PurchaseOrder purchaseOrder = purchaseOrderDAO.get(id);
		return purchaseOrder;
	}

	/**
	 * 新建商品信息
	 * 
	 * @param purchaseOrder
	 * @throws CRUDException
	 */
	public void savePurchaseOrder(PurchaseOrder purchaseOrder)
			throws CRUDException {
		purchaseOrder.setArriveState(JXCString.NOTARRIVED);
		purchaseOrder.setAuditState(JXCString.NOTAPPLY);
		purchaseOrder.setDeleteFlag("0");
		purchaseOrder.setCreateTime(new Date());
		purchaseOrder.setCreator(LoginBean.getLoginBean().getUser());
		purchaseOrderDAO.save(purchaseOrder);
	}

	/**
	 * 修改商品信息
	 * 
	 * @param purchaseOrder
	 * @throws CRUDException
	 */
	public void updatePurchaseOrder(PurchaseOrder purchaseOrder)
			throws CRUDException {
		PurchaseOrder oldPurchaseOrder = purchaseOrderDAO.get(purchaseOrder
				.getId());
		oldPurchaseOrder.setBillCode(purchaseOrder.getBillCode());
		oldPurchaseOrder.setBillRemark(purchaseOrder.getBillRemark());
		oldPurchaseOrder.setOrderDate(purchaseOrder.getOrderDate());
		oldPurchaseOrder.setPurchaser(purchaseOrder.getPurchaser());
		oldPurchaseOrder.setSupplier(purchaseOrder.getSupplier());
		oldPurchaseOrder.setTotalPrice(purchaseOrder.getTotalPrice());
		oldPurchaseOrder.setArriveState(purchaseOrder.getArriveState());
		oldPurchaseOrder.setAuditState(purchaseOrder.getAuditState());
		oldPurchaseOrder.setRemark(purchaseOrder.getRemark());
		oldPurchaseOrder.setEditor(LoginBean.getLoginBean().getUser());
		oldPurchaseOrder.setEditTime(new Date());
		purchaseOrderDAO.update(oldPurchaseOrder);
	}

	/**
	 * 批量删除采购订单
	 * 
	 * @param idList
	 * @return
	 * @throws CRUDException
	 */
	public void deleteProductOrder(String id) throws CRUDException {
		PurchaseOrder purchaseOrder = purchaseOrderDAO.get(id);
		purchaseOrder.setDeleteFlag("1");
		purchaseOrderDAO.update(purchaseOrder);
	}

}